<?php
/**
 * @title 877. 石子游戏 - 缓存先后手的值
 * @author start2004
 */

ini_set("memory_limit", "24M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[] $piles
     * @return Boolean
     */
    function stoneGame($piles) {
        /**
         * @since 2020-09-25 长度
         */
        $len = count($piles);

        /**
         * @since 2020-09-25 总和一半
         */
        $this->half = (array_sum($piles)) >> 1;

        /**
         * @since 2020-09-25 缓存结果
         */
        $this->dp = [];

        /**
         * @since 2020-09-25 获取先手最大值
         */
        $data = self::find($piles, 0, $len-1);

        /**
         * @return
         */
        return $data[0] > $data[1];
    }

    /**
     * @title 递归, 返回先手和后手最大值
     * @author start2004
     * @since 2020-09-25 Created
     *
     * @param array $nums
     * @param int $left
     * @param int $right
     * @param int $sum1
     * @param int $sum2
     * @return bool
     */
    public function find($nums = [], $left = 0, $right = 0){
        $dpKey = "{$left}-{$right}";
        if(isset($this->dp[$dpKey])){
            return $this->dp[$dpKey];
        } else {}

        /**
         * @since 2020-09-28 结果集
         */
        $result = [];

        /**
         * @since 2020-09-28 只有一个数
         */
        if($left == $right){
            $result = [$nums[$left], 0];
        } else {
            /**
             * @since 2020-09-28 先手数组第一个
             */
            $data = self::find($nums, $left+1, $right);
            $sum1 = $nums[$left] + $data[1];
            $sum2 = $data[0];

            /**
             * @since 2020-09-28 先手取数组最后一个
             */
            $data = self::find($nums, $left, $right-1);
            $tmp1 = $nums[$right] + $data[1];
            if($tmp1 > $sum1){
                $sum1 = $tmp1;
                $sum2 = $data[0];
            } else {}

            /**
             * @since 2020-09-28 返回先手较大值
             */
            $result = [$sum1, $sum2];
        }

        /**
         * @return
         */
        $this->dp[$dpKey] = $result;
        return $result;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.25_4.php
 */
$datas = [
    [[5,3,4,5]],
    [[37,70,48,31,71,41,68,94,94,32,66,11,67,54,50,78,63,59,21,46,47,39,39,93,11,90,90,16,55,40,4,38,62,21,10,25,49,28,19,5,91,63,62,55,21,77,4,81,41,17,74,67,89,56,56,10,92,65,20,44,4,71,23,50,11,56,34,84,59,73,45,48,73,83,2,23,84,72,37,11,86,2,8,1,51,72,13,15,39,5,30,29,69,67,19,44,10,21,69,25]],
    // [[69,13,93,93,76,72,58,55,79,49,4,71,53,16,86,16,39,11,19,19,3,1,36,4,100,60,64,59,29,27,31,73,1,22,95,71,62,6,99,89,83,12,3,8,39,89,47,74,20,10,32,18,6,69,100,18,96,12,88,43,72,98,12,34,35,37,29,23,21,76,48,88,3,13,88,24,42,57,11,52,100,71,25,77,79,57,88,73,40,95,100,59,29,94,5,17,78,25,47,65]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->stoneGame(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
